const dao = require('./../dao')

function getByUser(user) {
    let param = [user];
    let sql = 'select * from user where user = ? '
    let data_list = dao.query(sql, param);
    return data_list[0];
}
function getById(id) {
    let param = [id];
    let sql = 'select * from user where id = ? '
    let data_list = dao.query(sql, param);
    return data_list[0];
}

function list(data){
    let sql = `SELECT * FROM user `;
    let sql_where = [];
    let param = [];
    if(data.user){
        sql_where.push("user = ?");
        param.push(data.user);
    }
    if(data.status){
        sql_where.push("status = ?");
        param.push(data.status);
    }
    if(data.active){
        sql_where.push("active = ?");
        param.push(data.active);
    }
    if(sql_where.length > 0){
        sql += ' where '+sql_where.join(' and ')
    }
    let data_list = dao.query(sql, param);
    return data_list;
}

function listByPage(data){
    let pageSize = data.pageSize;
    let pageIndex = data.pageIndex;

    const offset = (pageIndex - 1) * pageSize;
    const limit = pageSize;
    let sql_count = `SELECT count(*) as total FROM user `;
    let sql = `SELECT * FROM user `;
    let sql_where = [];
    let param = [];
    if('user' in data){
        sql_where.push("user = ?");
        param.push(data.user);
    }
    if('status' in data){
        sql_where.push("status = ?");
        param.push(data.status);
    }
    if('active' in data){
        sql_where.push("active = ?");
        param.push(data.active);
    }
    if(sql_where.length > 0){
        sql_count += ' where '+sql_where.join(' and ')
        sql += ' where '+sql_where.join(' and ')
    }
    let total =  dao.query(sql_count, param)[0].total;

    sql += ' LIMIT ? OFFSET ? '
    param = param.concat([limit,offset]);
    let data_list = dao.query(sql, param);
    return {total:total,dataList:data_list}
}

function save(data){
    let t = new Date().getTime();
    let params = [t,t];
    params.push(data.user);
    params.push(data.pwd);
    params.push(data.status);
    params.push(data.active);
    params.push(data.cookie);
    let sql = "INSERT INTO user (create_time, update_time, user, pwd, status, active,cookie) VALUES (?, ?, ?, ?, ?, ?,?)";
    return dao.execute(sql, params);
}

function update(data){
    let t = new Date().getTime();
    let params = [t];
    params.push(data.user);
    params.push(data.pwd);
    params.push(data.status);
    params.push(data.active);
    params.push(data.cookie);
    params.push(data.id);
    let sql = "update user set update_time = ?, user=?, pwd=?, status=?, active=?,cookie=? where id =?";
    return dao.execute(sql, params);
}


function deleteById(data){
    let sql = "delete from user  where id =?";
    return dao.batchUpdate(sql,data.ids);
}

function setToken(data){
    let t = new Date().getTime();
    let params = [t];
    params.push(data.status);
    params.push(data.cookie);
    params.push(data.user);
    let sql = "update user set update_time = ?, status = ?,cookie=? where user =?";
    return dao.execute(sql, params);
}



module.exports = {
    getByUser,
    getById,
    list,
    listByPage,
    save,
    update,
    deleteById,
    setToken,
};
